import { formatTime } from '../../../utils/util.js';
const app = getApp()
Page({

  data: {
    order: {},
    buyer: {},
    modalVisible: false,
    form: {
      courier: "",//快递单号
    }
  },

  onLoad: function (options) {
    const id = options.id
    this.setData({ openid: app.openid, id })
  },
  onShow() {
    this.getOrderDetail(this.data.id)
    this.getNote();
  },
  async getOrderDetail(id) {
    const res = await app.request({
      url: 'getOrderDetail', method: "POST", data: { id }
    })
    if (res.meta.status >= 300) return wx.showToast({ title: "没有订单呢", icon: 'error' });
    const data = res.data
    data.order.create_at = formatTime(data.order.create_at)
    this.setData({
      order: data.order,
      buyer: data.buyer
    })
  },
  // 取消填写快递单号
  onCancelModalBtn() {
    this.setData({ modalVisible: false })
  },
  // 保存填写快递单号
  async onSaveModalBtn() {
    const { form } = this.data;
    if (!form.seller_name || form.seller_name.trim() === '') return app.showModal({ content: '请完善 姓名' })
    if (!form.seller_phone || form.seller_phone.trim() === '') return app.showModal({ content: '请完善 电话' })
    if (!form.courier || form.courier.trim() === '') return app.showModal({ content: '请完善 快递单号' })
    const res = await app.request({ url: `updateOrder`, data: { status: "已发货，待收货", id: this.data.order.id, ...form }, method: "POST" })
    if (res.meta.status >= 300) return app.showModal({ content: "保存失败 " + res.meta.msg })
    app.showModal({ content: "保存成功 " + res.meta.msg })
    this.getOrderDetail(this.data.id)
    this.setData({ modalVisible: false })
  },
  // 填写单号
  async onClickFillOrderFormBTn() {
    const { form, order } = this.data
    form.courier = order.courier
    form.seller_name = order.seller_name
    form.seller_phone = order.seller_phone
    this.setData({ modalVisible: true, form })
  },
  // 取消订单
  async onClickCancelAcceptBTn() {
    const modal = await app.showModal({ content: `确认取消接单？`, confirmText: `确认` })
    if (modal.cancel) return
    const res = await app.request({ url: `updateOrder`, data: { id: this.data.order.id, status: "审核通过，待接单", seller_openid: null, seller_name: null, seller_phone: null, courier: null }, method: "POST" })
    if (res.meta.status >= 300) return app.showModal({ content: "请求失败 " + res.meta.msg })
    await app.showModal({ content: "已取消接单" })
    this.getOrderDetail(this.data.id)
  },
  // 确认收货
  async onClickCheckAccept() {
    const modal = await app.showModal({ content: `确定确认收货？`, confirmText: `确认` })
    if (modal.cancel) return
    const res = await app.request({ url: `updateOrder`, data: { id: this.data.order.id, status: "已收货，待打钱" }, method: "POST" })
    if (res.meta.status >= 300) return app.showModal({ content: "请求失败 " + res.meta.msg })
    await app.showModal({ content: "已确认收货" })
    this.getOrderDetail(this.data.id)
  },
  onInput(e) {
    const { key } = e.target.dataset
    const { value } = e.detail
    const { form } = this.data
    form[key] = value
    this.setData({
      form
    })
  },
  // 确认接单
  async onClickAcceptBTn() {
    if (!await app.checkUserStatus()) return
    const modal = await app.showModal({ content: `确认接单？`, confirmText: `确认` })
    if (modal.cancel) return
    const res = await app.request({ url: `updateOrder`, data: { id: this.data.order.id, status: "已接单，待发货", seller_openid: app.openid }, method: "POST" })
    console.log(res);
    if (res.meta.status >= 300) return app.showModal({ content: "请求失败 " + res.meta.msg })
    await app.showModal({ content: "订单已接受，请尽快发货并填写快递单号" })
    this.getOrderDetail(this.data.id)
  },
  // 支付按钮
  onClickPayBtn() {
    const { order } = this.data
    wx.navigateTo({ url: `/pages/add/pay/pay?form=` + JSON.stringify(order), })
  },
  async onClickCancelOrderBtn() {

    const order = this.data.order
    let status = ""
    if (order.status.indexOf("取消") >= 0) return app.showModal({ content: "已经取消订单了哦！" })
    const modal = await app.showModal({ content: "确定取消订单？", confirmText: "确认" })
    if (modal.cancel) return
    if (order.status === "已下单，待付款") status = "未付款，取消订单"
    else if (order.status === "已付款，待审核") status = "已付款，取消订单"
    else if (order.status === "审核通过，待接单") status = "审核通过，取消订单"
    else return app.showModal({ content: "已经无法取消订单了哦！" })
    const res = await app.request({
      url: 'updateOrder', method: "POST", data: { id: this.data.id, status }
    })
    if (res.meta.status >= 300) return app.showModal({ content: "请求失败了呢：" + res.meta.msg });
    app.showModal({ content: "已经取消订单了哦！" })
    this.getOrderDetail(this.data.id)
  },
  // 获取留言
  async getNote() {
    const res = await app.request({ url: `getNote`, data: { order_id: this.data.id }, method: `POST` })
    if (res.meta.status >= 300) return app.showToast({ title: `获取留言失败` })
    const note = res.data.reverse()
    this.setData({ note })

  },
  // 输入留言
  onNoteInput(e){
    console.log(e);
    const value=e.detail.value  
    this.setData({note_content:value})
    
  },
  // 发送留言
  async onClickSendNote(){
    const  {note_content}=this.data
    if(!note_content||!note_content.trim()) return app.showModal({content:`留言不能为空哦！~`,showCancel:false})
    const modal=await app.showModal({content:`确认发送?`,confirmText:`确认`})
    if(modal.cancel)return
    const res=await app.request({url:`addNote`,data:{order_id:this.data.order.id,content:note_content,openid:app.openid},method:"POST"})
    console.log(res);
    if(res.meta.status>=300)return app.showModal({content:"发送失败:"+res.meta.msg})
    app.showModal({content:`发送成功`})
    this.getNote()    
    this.setData({note_content})
  },
})